package com.sub.controller;


import com.sub.pojo.Dict;
import com.sub.repository.DictRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictRepository dictRepository;

    @GetMapping("/findAll")
    public List<Dict> selectAllDict(){
        Sort sort = Sort.by(Sort.Direction.DESC,"create_time");
//        dictRepository.findAll();
        return dictRepository.findAll();
    }

    @GetMapping("/findOne")
    public Dict findOne(Integer id){
        return dictRepository.getOne(id);
    }

    @PostMapping("/add")
    public String addDict(Dict dict){
        dictRepository.save(dict);
        return "添加成功";
    }

    @DeleteMapping("/delete")
    public String deleteDictById(Integer id){
        dictRepository.deleteById(id);
        return "删除成功";
    }

    @PutMapping("/update")
    public String updateDict(Dict dict){
        dictRepository.deleteById(dict.getDictId());
        dictRepository.save(dict);
        return "修改成功";
    }

    @GetMapping("/findByXx")
    public Dict findByXX(String dictName){
        Dict byDictName = dictRepository.findByDictName(dictName);
        return byDictName;
    }

    @GetMapping("/findByDescription")
    public Dict findByDescription(String description){
        return dictRepository.findByDictDescription(description);
    }





}
